package com.shixi.service;

import com.shixi.dao.UserDao;

import com.shixi.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

@Service
public class UserService {

    @Autowired
    UserDao userDao;

    /*根据用户名和密码匹配，验证是否登录*/
    public boolean checkLogin(String username, String password, HttpServletRequest request)
    {
        User user = userDao.getUserByUP(username,password);

        /*如果查询到的用户不为null*/
        if(user!=null)
        {
            /*用户名和密码是正确*/
            /*可进行登录，用户信息存到session当中*/
            request.getSession().setAttribute("loginUser",user);
            return true;
        }
        else {
            return false;
        }
    }

    /*用户修改密码*/
    public boolean updatePwd(String username, String oldPwd, String newPwd, HttpServletRequest request){
        if(userDao.getUserByUP(username,oldPwd) == null){
            return false;
        }else{
            if(userDao.updateUserPwd(username,newPwd) > 0){
                User user = userDao.getUserByUP(username,newPwd);
                request.setAttribute("loginUser", user);
                return true;
            }else{
                return false;
            }
        }
    }

    /*用户修改手机号*/
    public boolean updatePhone(String username, String phone, HttpServletRequest request){
        if(userDao.updateUserPhone(username,phone) > 0){
            User user = (User) request.getSession().getAttribute("loginUser");
            user.setPhone(phone);
            request.setAttribute("loginUser", user);
            return true;
        }else{
            return false;
        }
    }
}
